package  
{
	import entities.Collectable;
	import entities.EndLevelTrigger;
	import entities.Enemy;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import org.flixel.*;
	/**
	 * The GameState is composed of two GameScenes. These are FlxStates which contain the entire level. The first 
	 * is the "real" world and the second is the "reflected" world. This works by having two identical copies
	 * of the world (so that at some point we can make them slightly less identical as a puzzle game mechanic)
	 * @author morgan
	 */
	public class GameScene extends FlxState
	{
		
		[Embed (source = "../data/tiles.png")] private var img_tiles:Class;
		[Embed (source = "../data/press_c.png")] private var img_press_c:Class;
		
		public var camera:FlxCamera;
		public var player:Player;
		public var tilemap:ReflectionMap;
		public var endTrigger:EndLevelTrigger;
		public var pickupObjectUI:FlxSprite;
		
		public var enemies:FlxGroup;
		public var throwables:FlxGroup;
		public var collidableEntities:FlxGroup;
		public var collectables:FlxGroup;
		public var gui:FlxGroup;
		
		public var matrix:Matrix;
		public var bgColor:uint = 0xffffffff;
		
		public var mapStr:String = "";
		
		public var real:Boolean = false;
		public var other:GameScene = null;
		
		public function GameScene() 
		{
			
		}
		
		override public function create():void 
		{
			super.create();
			
			camera = new FlxCamera(0, 0, FlxG.width * FlxG.camera.zoom, FlxG.height * FlxG.camera.zoom, FlxG.camera.zoom);
			camera.scroll.y = 0;
			camera.scroll.x = 160;
			
			enemies = new FlxGroup();
			throwables = new FlxGroup();
			collidableEntities = new FlxGroup();
			collectables = new FlxGroup();
			gui = new FlxGroup();			
			
			player = new Player(280, 80, this);
			
			pickupObjectUI = new ReflectionSprite(0, 0, this);
			pickupObjectUI.loadGraphic(img_press_c, true, false, 16, 16);
			pickupObjectUI.addAnimation("default", [0, 1], 4, true);
			pickupObjectUI.play("default");
			pickupObjectUI.visible = false;
			
			gui.add(pickupObjectUI);
			
			tilemap = new ReflectionMap(this);
			tilemap.loadMap(mapStr, img_tiles, 16, 16);
			
			if (endTrigger == null) { 
				trace("NO END TRIGGER!! Making one at random, you asshole.");
				endTrigger = new EndLevelTrigger(Math.random() * 10000, Math.random() * 10000, this);
			}
			
			FlxG.worldBounds = new FlxRect(0, 0, tilemap.width, tilemap.height);
		
			add(tilemap);
			add(player);
			add(enemies);
			add(collectables);
			add(collidableEntities);
			add(throwables);
			add(endTrigger);
			add(gui);
		}
		
		override public function update():void 
		{
			super.update();
			
			FlxG.collide(player, tilemap);
			FlxG.collide(throwables, tilemap);
			FlxG.collide(player, collidableEntities, playerCollidesEntity);
			FlxG.collide(throwables, collidableEntities);
			FlxG.overlap(player, collectables, playerOverlapsCollectable);
			FlxG.overlap(player, endTrigger, playerOverlapsEndTrigger);
			FlxG.overlap(player, enemies, playerOverlapsEnemy);
			FlxG.collide(enemies, tilemap);
			FlxG.overlap(throwables, enemies, throwableOverlapsEnemy);
		}
		
		public function throwableOverlapsEnemy(t:Throwable, e:Enemy):void
		{
			if ( Math.abs(t.velocity.x) > 20 || Math.abs(t.velocity.y) > 30 )
			{
				e.kill();
				t.velocity.x = -t.velocity.x / 2;
				t.velocity.y = -t.velocity.y / 2;
			}
		}
		
		public function playerCollidesEntity(p:Player, e:ReflectionSprite):void
		{
			p.collides(e);
			e.collides(p);
		}
		
		public function playerOverlapsEnemy(p:Player, e:Enemy):void
		{
			e.touchedPlayer(p);
		}
		
		public function playerOverlapsCollectable(p:Player, c:Collectable):void
		{
			c.collect(p);
		}
		
		public function playerOverlapsEndTrigger(p:Player, t:EndLevelTrigger):void
		{
			t.triggered = true;
			p.awaitTheEnd = true;
		}
		
		override public function draw():void 
		{
			camera.buffer.fillRect(new Rectangle(0, 0, FlxG.width, FlxG.height), bgColor);
			super.draw();
		}
	}

}